package com.youthburin.base.taglib;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.youthburin.base.util.DwzPagination;

public class DwzSearchFieldTag extends BaseTag {
	private static final long serialVersionUID = 1L;

	private String label;
	private String ops;
	private String name;
	private String fieldClass;

	private static String[] allOps = { "eq", "ne", "lt", "le", "gt", "ge", "bw", "ew", "cn" };
	private static String[] allOpsDesc = { "page.serarchfield.eq", "page.serarchfield.ne", "page.serarchfield.lt",
			"page.serarchfield.le", "page.serarchfield.gt", "page.serarchfield.ge", "page.serarchfield.bw",
			"page.serarchfield.ew", "page.serarchfield.cn" };

	@Override
	public int doEndTag() throws JspException {
		try {
			String opOption = getAllOperatorOption(ops);

			String searchFiledName = DwzPagination.SEARCHFILEDNAME_PREFIX + name;
			String searchFieldOpName = DwzPagination.SEARCHFIELDOPNAME_PREFIX + name;

			String searchFiledValue = StringUtils.trimToEmpty(pageContext.getRequest().getParameter(searchFiledName));

			StringBuffer str = new StringBuffer();
			str.append("<table class='searchFiledTable' style='width:100%;'>");
			str.append("<tr>");
			str.append("	<td width='40%' class='title'>");
			str.append("		<label for='").append(searchFiledName).append("'>").append(label).append("：</label>");
			str.append("	</td>");
			str.append("	<td width='10%'>");
			str.append("		<select style='width:5em;' class='combox' name='").append(searchFieldOpName).append("'>");
			str.append(StringUtils.isBlank(opOption) ? getOperatorOption("eq") : opOption);
			str.append("		</select>");
			str.append("	</td>");
			str.append("	<td width='50%'>");
			if (bodyContent != null) {
				str.append(bodyContent.getString());
			} else {
				str.append("		<input type='text' id='").append(searchFiledName).append("' name='")
						.append(searchFiledName).append("' class='searchField ")
						.append(StringUtils.trimToEmpty(fieldClass)).append("' value='").append(searchFiledValue)
						.append("'>");
			}
			str.append("	</td>");
			str.append("</tr>");
			str.append("</table>");

			pageContext.getOut().write(str.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	private String getAllOperatorOption(String ops) {
		if (StringUtils.isNotBlank(ops)) {
			StringBuffer sb = new StringBuffer();
			String[] opArray = ops.split(",");
			for (String op : opArray) {
				sb.append(getOperatorOption(op));
			}
			return sb.toString();
		}
		return "";
	}

	private String getOperatorOption(String op) {
		if (ArrayUtils.contains(allOps, op)) {
			String searchFieldOpName = DwzPagination.SEARCHFIELDOPNAME_PREFIX + name;
			String searchFieldOpValue = StringUtils.trimToEmpty(pageContext.getRequest()
					.getParameter(searchFieldOpName));

			int index = ArrayUtils.indexOf(allOps, op);

			String opdesc = getMessage(allOpsDesc[index]);
			if (StringUtils.equals(op, searchFieldOpValue)) {
				return ("<option selected value='" + op + "'>" + opdesc + "</option>");
			} else {
				return ("<option value='" + op + "'>" + opdesc + "</option>");
			}
		}
		return "";
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = getMessage(label);
	}

	public String getOps() {
		return ops;
	}

	public void setOps(String ops) {
		this.ops = ops;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFieldClass() {
		return fieldClass;
	}

	public void setFieldClass(String fieldClass) {
		this.fieldClass = fieldClass;
	}
}
